package com.bibrApp.difu.actvity.fund_security_code_mvp;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.bibrApp.difu.Config;
import com.bibrApp.difu.Response.LoginResponseBody;
import com.bibrApp.difu.api.RetrofitNet;
import com.bibrApp.difu.base.BaseObserver;
import com.bibrApp.difu.base.BasePresenter;
import com.bibrApp.difu.utils.DialogUtils;
import com.bibrApp.difu.utils.EncryptUtils;
import com.bibrApp.difu.utils.ToastUtils;
import com.google.gson.Gson;

import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class FundSecurityCodePresenter extends BasePresenter<IFundSecurityCodeView> {
    public FundSecurityCodePresenter(Context context, IFundSecurityCodeView iView) {
        super(context, iView);
    }

    /**
     * 设置资金安全码
     *
     * @param pwd
     */
    public void setFSCode(String pwd, String code) {
        DialogUtils.showDialog(context);
        HashMap<String, Object> map = new HashMap<>();
        map.put("tradePassword", pwd);
        if (!TextUtils.isEmpty(code)) {
            map.put("verifyCode", code);
        }
        RetrofitNet.getInstance()
                .getApi()
                .setFundSecurityCode(EncryptUtils.encryptNewType(map))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context,true) {
                    @Override
                    protected void onSuccess(Object value) {
                        iView.success("");
                        ToastUtils.ShowToast(context, "资金安全码设置成功");

                    }

                    @Override
                    protected void onSuccessOfCode(int code, String msg) {
                        super.onSuccessOfCode(code, msg);
                        ToastUtils.ShowToast(context, "资金安全码设置失败");
                    }
                });

    }

    /**
     * 获取验证码
     *
     * @param mobile
     */
    public void getCode(String mobile) {
        DialogUtils.showDialog(context);
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("noteType", Config.BASE_URL + Config.FUND_SECURITY_CODE_URL);
        RetrofitNet.getInstance()
                .getApi()
                .getVerifyCode(EncryptUtils.encryptNewType(map))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {

                    @Override
                    protected void onSuccess(Object value) {
                        if (value != null) {
                            iView.successOfCode(String.valueOf(value));
                            ToastUtils.ShowToast(context, String.valueOf(value));
                        }

                    }
                });

    }

    public void getUserInfo(String userId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        RetrofitNet.getInstance().getApi()
                .getAccountInfo(RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(map)))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        String jsonStr = EncryptUtils.objectToJson(value);
                        LoginResponseBody.CenterUserMain loginRes = new Gson().fromJson(jsonStr, LoginResponseBody.CenterUserMain.class);
                        iView.successOfGetUserInfo(loginRes);
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                    }
                });

    }
}
